import { login, logout, loginByCode, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = async () => {
  return {
    name: '',
    avatar: '',
    merchant_code: '',
    merchant_name: '',
    shop_code: '',
    shop_name: '',
    rq_code: '',
    rq_name: '',
    user_id: '',
    token: await getToken()
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_ACCESS_TOKEN: (state, access_token) => {
    state.access_token = access_token
  },
  SET_ACCESS_UID: (state, access_uid) => {
    state.access_uid = access_uid
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_MERCHANT_CODE: (state, merchant_code) => {
    state.merchant_code = merchant_code
  },
  SET_MERCHANT_NAME: (state, merchant_name) => {
    state.merchant_name = merchant_name
  },
  SET_SHOP_CODE: (state, shop_code) => {
    state.shop_code = shop_code
  },
  SET_SHOP_NAME: (state, shop_name) => {
    state.shop_name = shop_name
  },
  SET_RQ_CODE: (state, rq_code) => {
    state.rq_code = rq_code
  },
  SET_RQ_NAME: (state, rq_name) => {
    state.rq_name = rq_name
  },
  SET_USER_ID: (state, user_id) => {
    state.user_id = user_id
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password, remember } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        console.log('module/user -> login =>' + JSON.stringify(data))
        console.log(remember)
        // 1.记住密码
        if (remember === true) {
          localStorage.setItem('login_remember', remember)
          localStorage.setItem('login_name', username)
          localStorage.setItem('password', password)
        }
        // 2.保存信息
        commit('SET_ACCESS_TOKEN', data.access_token)
        commit('SET_ACCESS_UID', data.access_uid)
        setToken(data.access_token, data.access_uid)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user loginByCode
  loginByCode({ commit }, userInfo) {
    const { inviteCode, remember } = userInfo
    return new Promise((resolve, reject) => {
      loginByCode({ inviteCode: inviteCode }).then(response => {
        const { data } = response
        console.log('module/user -> loginByCode =>' + JSON.stringify(data))
        console.log(remember)
        // 1.记住密码
        if (remember === true) {
          localStorage.setItem('invite_code_remember', remember)
          localStorage.setItem('invite_code', inviteCode)
        }

        commit('SET_ACCESS_TOKEN', data.access_token)
        commit('SET_ACCESS_UID', data.access_uid)

        setToken(data.access_token, data.access_uid)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo().then(response => {
        const { data } = response
        if (!data) {
          reject('验证失败，请重新登录.')
        }
        console.log('module/user -> getInfo =>' + JSON.stringify(data))
        const { name, avatar, rq_code, rq_name, merchant_code, merchant_name, shop_code, shop_name, user_id } = data

        commit('SET_NAME', data.user_name)
        commit('SET_AVATAR', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif')

        commit('SET_RQ_CODE', data.rq_code)
        commit('SET_RQ_NAME', data.rq_name)
        commit('SET_MERCHANT_CODE', data.merchant_code)
        commit('SET_MERCHANT_NAME', data.merchant_name)
        commit('SET_SHOP_CODE', data.shop_code)
        commit('SET_SHOP_NAME', data.shop_name)
        commit('SET_USER_ID', data.user_id)

        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

